<!-- 公共文件 start -->
@extends('admin.public.base')
<!-- 公共文件 end -->

@section('content')
    <style>
        .grid-demo {
            margin: 0 auto;
            width: 100px;
        }

        .grid-demo-btn1 {
            width: 30px;
            height: 30px;
            border-radius: 50%;
            border: 0px;
            background-color: #cccccc;
            color: #FFFFFF
        }

        .grid-demo-txt1 {
            color: #cccccc
        }
    </style>

    <div class="layui-row" style="margin: 20px 0px 10px 0px;">
        <div class="layui-col-xs4">
            <div class="grid-demo">
                <button type="button" class="grid-demo-btn1 step1">1</button>
                <span class="grid-demo-txt1 stept1">基本信息</span>
            </div>
        </div>
        <div class="layui-col-xs4">
            <div class="grid-demo">
                <button type="button" class="grid-demo-btn1 step2">2</button>
                <span class="grid-demo-txt1 stept2">财务信息</span>
            </div>
        </div>
        <div class="layui-col-xs4">
            <div class="grid-demo">
                <button type="button" class="grid-demo-btn1 step3">3</button>
                <span class="grid-demo-txt1 stept3">产品信息</span>
            </div>
        </div>
    </div>
    <div class="layui-progress" style="margin-bottom: 10px">
        <div class="layui-progress-bar"></div>
    </div>
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form">
                <div class="step01">
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class="x-red">*</span>代理商编号
                        </label>
                        <div class="layui-input-inline">
                            <input type="text" name="store_sn" readonly value="{{ $data['store_sn'] }}"
                                   class="layui-input layui-disabled">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">
                            <span class="x-red">*</span>代理商名
                        </label>
                        <div class="layui-input-inline">
                            <input type="text" name="store_name" value="{{ $data['store_name'] }}" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span class="x-red">*</span>所属子公司</label>
                        <div class="layui-input-inline">
                            <select name="subsidiary" lay-verify="required" lay-reqtext="请选择所属子公司">
                                <option value="">请选择</option>
                                <option value="上海有利康商贸有限公司" @if(isset($data['subsidiary']) && $data['subsidiary']=='上海有利康商贸有限公司') selected @endif>上海有利康商贸有限公司</option>
                                <option value="亿恩鑫（上海）生物科技有限公司" @if(isset($data['subsidiary']) && $data['subsidiary']=='亿恩鑫（上海）生物科技有限公司') selected @endif>亿恩鑫（上海）生物科技有限公司</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><b>个人信息：</b></label>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">法人身份证</label>
                        <div class="layui-input-inline layui-upload">
                            <button type="button" class="layui-btn" id="aa1">上传身份证人像面</button>
                            <div class="layui-upload-list">
                                <input name="principal_card_a" value="{{ $data['principal_card_a'] }}" id="vv1"
                                       type="hidden">
                                <img class="layui-upload-img" src="{{ $data['principal_card_a'] }}" id="dd1"
                                     width="150px" height="100px">
                            </div>
                        </div>
                        <div class="layui-input-inline layui-upload">
                            <button type="button" class="layui-btn" id="aa2">上传身份证国徽面</button>
                            <div class="layui-upload-list">
                                <input name="principal_card_b" value="{{ $data['principal_card_b'] }}" id="vv2"
                                       type="hidden">
                                <img class="layui-upload-img" src="{{ $data['principal_card_b'] }}" id="dd2"
                                     width="150px" height="100px">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="principal_name" value="{{ $data['principal_name'] }}"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>电话</label>
                            <div class="layui-input-inline">
                                <input type="text" name="principal_mobile" value="{{ $data['principal_mobile'] }}"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">邮件</label>
                            <div class="layui-input-inline">
                                <input type="text" name="principal_email" value="{{ $data['principal_email'] }}"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>地址</label>
                            <div class="layui-input-inline">
                                <input type="text" name="principal_address" value="{{ $data['principal_address'] }}"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>身份证号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="principal_card_sn" value="{{ $data['principal_card_sn'] }}"
                                       class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label"><b>公司资料：</b></label>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">企业证件</label>
                        <div class="layui-input-inline layui-upload">
                            <button type="button" class="layui-btn" id="aa3">上传营业执照</button>
                            <div class="layui-upload-list">
                                <input name="company_license" value="{{ $data['company_license'] }}" id="vv3"
                                       type="hidden">
                                <img class="layui-upload-img" src="{{ $data['company_license'] }}" id="dd3"
                                     width="150px" height="100px">
                            </div>
                        </div>
                        <div class="layui-input-inline layui-upload">
                            <button type="button" class="layui-btn" id="aa4">上传营运许可证</button>
                            <div class="layui-upload-list">
                                <input name="company_license_yy" value="{{ $data['company_license_yy'] }}" id="vv4"
                                       type="hidden">
                                <img class="layui-upload-img" src="{{ $data['company_license_yy'] }}" id="dd4"
                                     width="150px" height="100px">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>公司名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="company_name" value="{{ $data['company_name'] }}"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>注册号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="company_sn" lay-verify="required" lay-reqtext="必填项,请填写信息"
                                       value="{{ $data['company_sn'] }}" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">公司性质</label>
                            <div class="layui-input-inline">
                                <input type="text" name="company_type" value="{{ $data['company_type'] }}"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">公司地址</label>
                            <div class="layui-input-inline">
                                <input type="text" name="company_addrss" value="{{ $data['company_addrss'] }}"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">法人代表</label>
                            <div class="layui-input-inline">
                                <input type="text" name="company_corporation" value="{{ $data['company_corporation'] }}"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">注册资本</label>
                            <div class="layui-input-inline">
                                <input type="text" name="company_registered_capital"
                                       value="{{ $data['company_registered_capital'] }}" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">成立日期</label>
                            <div class="layui-input-inline">
                                <input type="text" name="company_create_time" value="{{ $data['company_create_time'] }}"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">营业期限</label>
                            <div class="layui-input-inline">
                                <input type="text" name="company_operating_period"
                                       value="{{ $data['company_operating_period'] }}" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-block">
                            <label class="layui-form-label">经营范围</label>
                            <div class="layui-input-block">
                                <textarea placeholder="请输入内容" class="layui-textarea"
                                          name="company_business_scope">{{ $data['company_business_scope'] }}</textarea>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>资质期限</label>
                            <div class="layui-input-inline">
                                <input type="text" name="agent_start_time" placeholder="开始日" id="start" value="{{ $data['agent_start_time'] }}" class="layui-input">
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="agent_end_time" placeholder="结束日" id="end" value="{{ $data['agent_end_time'] }}" class="layui-input">
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>允许一定金额内免审核</label>
                            <div class="layui-input-inline">
                                <input type="number"
                                       name="avoid_review_amount"
                                       value="{{ old('avoid_review_amount', $data['avoid_review_amount']) }}"
                                       min="0.01"
                                       step="0.01"
                                       lay-verify="required|number"
                                       placeholder="> 0"
                                       class="layui-input">
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">免审核时段</label>
                            <div class="layui-input-inline">
                                <input type="text"
                                       name="avoid_review_start_time"
                                       id="avoid_review_start_time"
                                       placeholder="开始日期时间"
                                       autocomplete="off"
                                       class="layui-input"
                                       value="{{ isset($data['avoid_review_start_time']) ? $data['avoid_review_start_time'] : '' }}">
                            </div>
                            <div class="layui-form-mid">至</div>
                            <div class="layui-input-inline">
                                <input type="text"
                                       name="avoid_review_end_time"
                                       id="avoid_review_end_time"
                                       placeholder="结束日期时间"
                                       autocomplete="off"
                                       class="layui-input"
                                       value="{{ isset($data['avoid_review_end_time']) ? $data['avoid_review_end_time'] : '' }}">
                            </div>
                        </div>
                    </div>


                    <div class="layui-form-item">
                        <div class="layui-block">
                            <label class="layui-form-label">备注</label>
                            <div class="layui-input-block">
                                <textarea placeholder="请输入内容" class="layui-textarea"
                                          name="remark">{{ $data['remark'] }}</textarea>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="L_repass" class="layui-form-label"></label>
                        <button class="layui-btn" type="button" onclick="toStepNum(2)">下一步</button>
                    </div>
                </div>
                <div class="step02">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>账号名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="user_name" value="{{ $data['store_sn'] }}"
                                       class="layui-input layui-disabled">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>密码</label>
                            <div class="layui-input-inline">
                                <input type="text" name="user_password" lay-verify="required" value=""
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>手机号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="user_mobile" value="" lay-reqtext="必填项,请填写信息"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="x-red">*</span>基础折扣率</label>
                            <div class="layui-input-inline">
                                <input type="text" name="discount_rate" value="1" class="layui-input discount_rate">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">货款余额</label>
                            <div class="layui-input-inline">
                                <input type="number" name="money_goods" value="0" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">管理费余额</label>
                            <div class="layui-input-inline">
                                <input type="number" name="money_management" value="0" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline" pane="">
                            <label class="layui-form-label">是否补货</label>
                            <div class="layui-input-block">
                                <input type="checkbox" checked="" value="1" name="is_replenishment" lay-skin="switch"
                                       lay-text="开启|关闭">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline" pane="">
                            <label class="layui-form-label">运费收取</label>
                            <div class="layui-input-block">
                                <input type="checkbox" checked="" value="1" name="is_express_fee" lay-skin="switch"
                                       lay-text="开启|关闭">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline" pane="">
                            <label class="layui-form-label">一件代发</label>
                            <div class="layui-input-block">
                                <input type="checkbox" name="is_agent" value="1" lay-skin="switch" lay-text="开启|关闭">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label for="L_repass" class="layui-form-label"></label>
                        <button class="layui-btn" type="button" onclick="toStepNum(1)">上一步</button>
                        <button class="layui-btn" type="button" onclick="toStepNum(3)">下一步</button>
                    </div>
                </div>
                <div class="step03">
                    <div class="layui-form-item">
                        <div class="layui-inline" pane="">
                            <label class="layui-form-label">基础折扣率</label>
                            <div class="layui-input-block">
                                <input type="number" readonly value="1" class="layui-input rateBase">
                            </div>
                        </div>
                    </div>
                    <div class="layui-card-body layui-table-body layui-table-main">
                        <table class="layui-table layui-form">
                            <thead>
                            <tr>
                                <th>图片</th>
                                <th>商品名</th>
                                <th>商品分类</th>
                                <th>商品编号</th>
                                <th>标准价格</th>
                                <th>设定价格</th>
                                <th>最低起订量</th>
                                <th>销售策略(买)</th>
                                <th>销售策略(赠)</th>
                            </tr>
                            </thead>
                            <tbody>
                            @foreach( $goodsList as $kk => $item)
                                <tr>
                                    <td><img src="{{ $item['goods_img'] }}" height="40px" alt="品牌图"></td>
                                    <td>{{ $item['goods_name'] }}</td>
                                    <td>{{ $item['cate_name'] }}</td>
                                    <td>{{ $item['goods_sn'] }}</td>
                                    <td class="goods_price_{{$kk}}">{{ $item['goods_price'] }}</td>
                                    <td>
                                        @if ($item['fixed_price'] == 1 || $item['promotion'] == 1)
                                            <input type="number" readonly name="goods[{{$kk}}][sale_price]"
                                                   value="{{$item['goods_price']}}"
                                                   class="layui-input layui-disabled">
                                        @else
                                            <input type="number" name="goods[{{$kk}}][sale_price]"
                                                   value="{{$item['goods_price']}}"
                                                   class="layui-input sale_price_{{$kk}}">
                                        @endif
                                    </td>
                                    <td>
                                    <input type="number" name="goods[{{$kk}}][mini_quantity]"
                                           value="{{$item['mini_quantity']}}" class="layui-input" min='1'>
                                    </td>
                                    <td>
                                        <input type="number" name="goods[{{$kk}}][buy_num]" value="1"
                                               class="layui-input">
                                    </td>
                                    <td>
                                        <input type="number" name="goods[{{$kk}}][get_num]" value="0"
                                               class="layui-input">
                                        <input type="hidden" name="goods[{{$kk}}][goods_id]" value="{{$item['id']}}">
                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                    <div class="layui-form-item">
                        <label for="L_repass" class="layui-form-label"></label>
                        <input type="hidden" value="{{ $data['id'] }}" name="id">
                        <button class="layui-btn" type="button" onclick="toStepNum(2)">上一步</button>
                        <button class="layui-btn" lay-filter="add" lay-submit="">提交</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <script>
        $('.step02').hide();
        $('.step03').hide();
        var stepNum = 1;
        var goodsNum = parseInt("{{$kk}}}");

        function toStepNum(num) {
            if (stepNum == 1 && num == 2) {
                if (!inputMust(['store_sn', 'store_name', 'principal_name', 'principal_mobile','agent_start_time', 'agent_end_time'])) {
                    return false;
                }
            } else if (num == 3) {
                if (stepNum == 2) {
                    if (!inputMust(['user_password', 'user_mobile', 'discount_rate'])) {
                        return false;
                    }
                }
                var goods_price = 0;
                var discount_rate = $('.discount_rate').val();
                console.log(discount_rate);
                $('.rateBase').val(discount_rate);
                for (i = 0; i <= goodsNum; i++) {
                    goods_price = $('.goods_price_' + i).html();
                    if (goods_price > 0 && discount_rate != 1) {
                        $('.sale_price_' + i).val(roundFun(goods_price * discount_rate, 2));
                    }
                }
            }
            $('.step01').hide();
            $('.step02').hide();
            $('.step03').hide();
            $('.step0' + num).show();
            addCss(num);
            stepNum = num;
        }

        function inputMust(myClass) {
            for (i = 0; i < myClass.length; i++) {
                var name = $('input[name="' + myClass[i] + '"]').val();
                if (!name || name == 'undefined') {
                    layer.msg('必填项不能为空！');
                    return false;
                }
            }
            return true;
        }

        addCss(1);
        function addCss(num) {
            var nnn = num * 33;
            if (num <= 2) {
                $(".step2").css({"background-color": "#cccccc"});
                $(".stept2").css({"color": "#cccccc"});
                $(".step3").css({"background-color": "#cccccc"});
                $(".stept3").css({"color": "#cccccc"});
            }
            $(".step" + num).css({"background-color": "#1ABC9C"});
            $(".stept" + num).css({"color": "#1ABC9C"});

            $(".layui-progress-bar").css({"width": nnn + "%","background-color": "#1ABC9C"});
        }

        layui.use('upload', function () {
            var $ = layui.jquery, upload = layui.upload;

            //普通图片上传
            var uploadInst = upload.render({
                elem: '#aa1'
                , url: '/admin/index/imageUpload' //改成您自己的上传接口
                , headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')}
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#dd1').attr('src', result); //图片链接（base64）
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code == 1) {
                        $('#vv1').val(res.data.image);
                    } else {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                }
            });
            //普通图片上传
            var uploadInst = upload.render({
                elem: '#aa2'
                , url: '/admin/index/imageUpload' //改成您自己的上传接口
                , headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')}
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#dd2').attr('src', result); //图片链接（base64）
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code == 1) {
                        $('#vv2').val(res.data.image);
                    } else {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                }
            });
            //普通图片上传
            var uploadInst = upload.render({
                elem: '#aa3'
                , url: '/admin/index/imageUpload' //改成您自己的上传接口
                , headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')}
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#dd3').attr('src', result); //图片链接（base64）
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code == 1) {
                        $('#vv3').val(res.data.image);
                    } else {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                }
            });
            //普通图片上传
            var uploadInst = upload.render({
                elem: '#aa4'
                , url: '/admin/index/imageUpload' //改成您自己的上传接口
                , headers: {'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')}
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#dd4').attr('src', result); //图片链接（base64）
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code == 1) {
                        $('#vv4').val(res.data.image);
                    } else {
                        return layer.msg('上传失败');
                    }
                    //上传成功
                }
            });
        });
        layui.use(['form', 'layer', 'jquery'], function () {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;

            //监听提交
            form.on('submit(add)', function (data) {
                console.log(data);
                //发异步，把数据提交给php
                $.ajax({
                    type: 'POST',
                    url: '/admin/store/storeAdd',
                    data: data.field,
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function (data) {
                        layer.alert(data.msg)
                        if (data.code == 1) {
                            //关闭当前frame
                            xadmin.close();
                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        }
                    },
                    error: function (xhr, type) {
                        alert('Ajax error!')
                    }
                });

                return false;
            });

        });
        layui.use('laydate',
            function () {
                var laydate = layui.laydate;

                //执行一个laydate实例
                laydate.render({
                    elem: '#start', //指定元素
                    type: 'datetime'
                });

                //执行一个laydate实例
                laydate.render({
                    elem: '#end', //指定元素
                    type: 'datetime'
                });
            });

        //保留n位小数
        function roundFun(value, n) {
            return Math.round(value * Math.pow(10, n)) / Math.pow(10, n);
        }
    </script>

    <script>
        layui.use(['laydate', 'jquery'], function() {
            var laydate = layui.laydate;
            var $ = layui.jquery;

            // 从 input 中获取后台返回的值
            var startDateValue = $('#avoid_review_start_time').val();
            var endDateValue = $('#avoid_review_end_time').val();

            // 开始日期时间选择器
            var startDatePicker = laydate.render({
                elem: '#avoid_review_start_time',
                type: 'datetime',
                format: 'yyyy-MM-dd HH:mm:ss',
                theme: '#448AFF',
                value: startDateValue,  // 仅使用后台数据，没有则留空
                max: endDateValue,
                ready: function() {
                    this.elemItem.querySelector('.layui-laydate-content').style.width = '180px';
                },
                change: function(value, date) {
                    if (date) {
                        endDatePicker.config.min = value;
                        endDatePicker.render();
                    }
                }
            });

            // 结束日期时间选择器
            var endDatePicker = laydate.render({
                elem: '#avoid_review_end_time',
                type: 'datetime',
                format: 'yyyy-MM-dd HH:mm:ss',
                theme: '#448AFF',
                value: endDateValue,  // 仅使用后台数据，没有则留空
                min: startDateValue,
                ready: function() {
                    this.elemItem.querySelector('.layui-laydate-content').style.width = '180px';
                },
                change: function(value, date) {
                    if (date) {
                        startDatePicker.config.max = value;
                        startDatePicker.render();
                    }
                }
            });
        });
    </script>


@endsection
